package com.lwk;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

public class BaiDuReader {
    public static void main(String[] args) throws IOException {

		String urlHost = "https://dushu.baidu.com/api/pc/getCatalog?data=";

        String urlBody = URLEncoder.encode("{\"book_id\":\"4356349964\"}", StandardCharsets.UTF_8);

        String url = urlHost + urlBody;

		Connection.Response res = Jsoup.connect(url)
				.header("Accept", "*/*")
				.header("Accept-Encoding", "gzip, deflate")
				.header("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
				.header("Content-Type", "application/json;charset=UTF-8")
				.header("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0")
				.timeout(10000).ignoreContentType(true).execute();//.get()

		String body = res.body();
        JSONArray jsonArray = JSON.parseObject(body).getJSONObject("data").getJSONObject("novel").getJSONArray("items");
        for (Object o : jsonArray) {
            JSONObject jsonObject = JSONObject.parseObject(String.valueOf(o));
            save(jsonObject.getString("cid"),jsonObject.getString("title"));
        }
    }

    private static void save( String cid, String title) throws IOException {
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream("D:\\result\\重生1983.md", true)));

            String urlBody = URLEncoder.encode("{\"book_id\":\"4356349964\",\"cid\":\"4356349964|"+cid+"\",\"need_bookinfo\":0}", StandardCharsets.UTF_8);
            String urlHost = "https://dushu.baidu.com/api/pc/getChapterContent?data=";
            String url = urlHost + urlBody;

            Connection.Response res = Jsoup.connect(url)
                    .header("Accept", "*/*")
                    .header("Accept-Encoding", "gzip, deflate")
                    .header("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
                    .header("Content-Type", "application/json;charset=UTF-8")
                    .header("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0")
                    .timeout(10000).ignoreContentType(true).execute();

            String body = res.body();
            String content = JSON.parseObject(body).getJSONObject("data").getJSONObject("novel").getString("content");
            out.write("\n\n\n\n## "+title+"\n\n```java\n\t"+content+"\n```\n");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            assert out != null;
            out.close();
            System.out.println(title + "获取完成！");
        }
        //需要使用休眠，为了防止网站检测为蓄意攻击，停止我们的IP访问
//        int n = (int) (Math.random() * 10 + 100);
//        try {
//            Thread.sleep(n);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
    }
}
